
$(document).ready(function () {
    /*var $row = $('.row');
    if ($row.length > 0) {
        $row.each(function (i) {
            var children = $(this).children();
            var that = $(this);
            var isSpaceBetween = false;
            var classArr = $(this).attr('class').split(' ').join('');
            // var classArr = $(this).attr('class')
            if (classArr.indexOf('space-between') > 0) {
                // 有space-between 类名
                isSpaceBetween = true;
            }
            if (isSpaceBetween && children.length > 0) {
                children.each(function (i) {
                    var borderWidth = $(this).css('border-width').split('px')[0];
                    var width = that.width() / children.length - borderWidth * 2 - 40;
                    $(this).css({
                        // "width": `${ width }`
                        "width": width + 'px',
                        "margin": '0 20px'
                    })
                })
            }
        })
    }*/
    // 表格-斑马线
    var $stripe = $('.stripe')
    if($stripe.length) {
        $stripe.each(function () {
            var tr = $(this).find('tr')
            if(tr.length) {
                tr.each(function (i) {
                    if (i===0) return
                    if(i % 2 === 0 ) {
                        console.log($(this).find('td'))
                        $(this).find('td').addClass('bg_gray')
                    }
                })
            }
            // $(this).css()
        })
    }

    $.fn.extend({
        model: function (options) {
            var defaults = {
                type: 'confirm',
                size: {width: 500, height: 300},
                ok: undefined,
                show: false,
                btnSize: 'size_mn',
                foo: true,
                title: "标题"
            };
            $.extend(defaults, options);
            var $this = $(this);
            // 设置标题
            var title = defaults.title || '';
            var model_header = '<div class="model_header clearfix"><span class="header_text">' + title + '</span>' + '<span class="model_close_btn">×</span></div>';
            $this.append(model_header)
                .find('.model_close_btn')
                .click(function (i) {
                    $(this).modelHide();
                });
            // 设置宽度高度
            $this.css({
                "width": defaults.size.width + 'px',
                "height": (defaults.size.height > 140 ? defaults.size.height : 140) + 'px'
            });
            // 脚
            if (defaults.foo) {
                var foo = '<div class="model_footer">' +
                    '        <div class="model_btns">' +
                    '            <div class="button cancel" style="margin-right: 10px">取消</div>' +
                    '            <div class="button success">确定</div>' +
                    '        </div>' +
                    '    </div>';
                // 取消确定按钮点击事件
                $this.append(foo)
                    .find('.model_btns')
                    .click(function (e) {
                        var className = e.target.className.split(' ')[1];
                        if (className === 'cancel') {
                            $(this).modelHide();
                        } else if (className === 'success') {
                            if (defaults.ok !== undefined && typeof (defaults.ok) === "function") {
                                defaults.ok();
                                $(this).modelHide();
                            }
                        }
                    });
                $this.find('.model_btns .button').addClass(defaults.btnSize)

            }
            if(defaults.type === "message") {
                $this.find('.model_header').css('border-bottom', 'none');
                $this.find('.model_footer').css('border-top', 'none');
            }
            if (defaults.show) {
                //$this.modelShow();
            }
            return this;
        },
        modelShow: function () {
            var mask = $("#mask");
            if (mask.length === 0) {
                mask = $('<div id="mask" class="mask"></div>');
                $("body").append(mask);
                mask.css({
                    "width": $(window).width(),
                    "height": $(window).height()
                });
            } else {
                mask.css('display', 'block')
            }
            var $this = $(this);
            // 弹框位置
            var margin_top = $(window).height() / 2 - $this.outerHeight() / 2;
            var margin_left = $(window).width() / 2 - $this.outerWidth() / 2;
            $this.css({
                "marginTop": margin_top + 'px',
                "marginLeft": margin_left + 'px'
            });

            // content 高度计算
            var contentHeight = ($this.height() - $this.find('.model_header').outerHeight() - $this.find('.model_footer').outerHeight() - 2) + 'px';
            $this.find('.content').css({
                'height': contentHeight,
                'overflow': 'auto'
            });
            $this.show();
            return this;
        },
        modelHide: function () {
            $('#mask').hide();
            $(this).parents('.model').hide();
            return this;
        }
    });
    $.fn.extend({
        onOff: function (options) {
            var defaults = {
                activeText: '开',
                inactiveText: '关',
                defaultValue: false,
                getValue: undefined
            };
            $.extend(defaults, options);
            var $this = $(this);
            var switchContent = '<span class="inactive">'+ defaults["inactiveText"] +'</span>\n' +
                '        <div class="switchBox clearfix">\n' +
                '            <img src="../../static/lib/image/icon/switch2.png" alt="">\n' +
                '        </div>\n' +
                '        <span class="active">'+ defaults["activeText"] +'</span>';
            $this.append(switchContent);
            if(!defaults.defaultValue) {
                $('.switchBox img').removeClass('open');
                defaults.defaultValue = true;
            } else {
                $('.switchBox img').addClass('open');
                defaults.defaultValue = false;
            }
            $this.find('.switchBox').click(function () {
                if(!defaults.defaultValue) {
                    $this.find('.switchBox img').removeClass('open');
                    defaults.defaultValue = true;
                } else {
                    $this.find('.switchBox img').addClass('open');
                    defaults.defaultValue = false;
                }
                if (typeof (defaults.getValue) === "function") {
                    defaults.getValue(!defaults.defaultValue);
                }
            })
        }
    })
});

